package com.primb.bench.project.home.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.primb.baselibs.util.FormatUtils;
import com.primb.bench.R;
import com.primb.bench.project.home.entity.EnsureObjectEntity;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LoanEnsureMenuAdapter extends SwipeMenuAdapter<LoanEnsureMenuAdapter.DefaultViewHolder> {

    private List<EnsureObjectEntity> dataList;

    private BaseQuickAdapter.OnItemClickListener mOnItemClickListener;

    private BaseQuickAdapter.OnItemChildClickListener mOnItemChildClickListener;
    private boolean isEdit;

    public LoanEnsureMenuAdapter(List<EnsureObjectEntity> dataList, boolean isEdit) {
        this.dataList = dataList;
        this.isEdit = isEdit;
    }

    public void setOnItemClickListener(BaseQuickAdapter.OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public void setOnItemChildClickListener(BaseQuickAdapter.OnItemChildClickListener onItemChildClickListener) {
        this.mOnItemChildClickListener = onItemChildClickListener;
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.size();
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ensure_info, parent, false);
    }

    @Override
    public DefaultViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new DefaultViewHolder(realContentView);
    }

    @Override
    public void onBindViewHolder(DefaultViewHolder holder, int position) {
        holder.setData(dataList.get(position), isEdit);
        holder.setOnItemClickListener(mOnItemClickListener);
        holder.setOnItemChildClickListener(mOnItemChildClickListener);
    }

    static class DefaultViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private BaseQuickAdapter.OnItemClickListener mOnItemClickListener;
        private BaseQuickAdapter.OnItemChildClickListener mOnItemChildClickListener;
        @BindView(R.id.tv_item_ensure_way)
        TextView tvItemEnsureWay;
        @BindView(R.id.iv_ensure_edit)
        ImageView ivEnsureEdit;
        @BindView(R.id.tv_item_ensure_type)
        TextView tvItemEnsureType;
        @BindView(R.id.tv_item_ensure_money_num)
        TextView tvItemEnsureMoneyNum;

        public DefaultViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(this);
        }

        public void setOnItemClickListener(BaseQuickAdapter.OnItemClickListener onItemClickListener) {
            this.mOnItemClickListener = onItemClickListener;
        }

        public void setOnItemChildClickListener(BaseQuickAdapter.OnItemChildClickListener onItemChildClickListener) {
            this.mOnItemChildClickListener = onItemChildClickListener;
        }

        public void setData(EnsureObjectEntity entity, boolean isEdit) {
            Double integer = Double.valueOf(entity.getPawnAmt());
            String format2Double = FormatUtils.format2Double(integer);
            tvItemEnsureMoneyNum.setText(format2Double);
            tvItemEnsureType.setText(entity.getGuatantDesc());
            tvItemEnsureWay.setText(entity.getPawnDesc());
            if (isEdit) {
                ivEnsureEdit.setOnClickListener(this);
                ivEnsureEdit.setVisibility(View.VISIBLE);
            } else {
                ivEnsureEdit.setVisibility(View.GONE);
            }
        }

        @Override
        public void onClick(View v) {

            if (mOnItemClickListener != null && v.getId() == itemView.getId()) {
                mOnItemClickListener.onItemClick(null, itemView, getAdapterPosition());
                return;
            }

            if (mOnItemChildClickListener != null) {
                mOnItemChildClickListener.onItemChildClick(null, v, getAdapterPosition());
                return;
            }
        }
    }
}
